package com.letv.whatslive.model.dto;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.Constants;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.User;
import com.letv.whatslive.model.constant.UserConstants;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by gaoshan on 15-7-3.
 */
@Getter
@Setter
public class UserDTO {

    private Long userId;
    private String nickName;
    private Integer sex;
    private String email;
    private String userIcon;
    private String introduce;
    private Integer role;
    private String recommend;
    private Long follower;
    private Long following;
    private Integer status;
    private Long likeNum;
    private String userName;
    private Integer userType; //1:facebook  2:twitter  3:system  4:微博 5:QQ 6:微信
    private String picture;
    private String phone;
    private String mobile;
    private Integer userStatus; //用户状态 1:正常 0:禁用
    private String address;
    private Long createTime;
    private Long updateTime;
    private Long lastLoginTime;
    private String accessToken; //第三方登陆的token
    private String thirdId;  //第三方账号Id
    private String sinaId;
    private String qqId;
    private String weixinId;
    private Integer sinaBind;
    private Integer qqBind;
    private Integer weixinBind;
    private String tokenSecret;  //twitter登陆的secret
    private Integer level; //用户等级,0为没有等级，1-5为不同的级别
    private Long broadCastNum; //直播次数
    private List<String> devIdList;
    private String ssoTk;
    private List<Map<String, Object>> tags; //用户分类
    private String logo;//用户视频水印
    private Integer weiDianStatus; //用户关联微店状态，0表示关闭，1表示开启

    public UserDTO(User user){
        this.setUserId(ObjectUtils.toLong(user.getUserId(),0l));
        this.setNickName(user.getNickName());
        this.setRole(ObjectUtils.toInteger(user.getRole(),0));
        this.setSex(ObjectUtils.toInteger(user.getSex(),0));
        this.setEmail(user.getEmail());
        this.setIntroduce(ObjectUtils.toString(user.getIntroduce(),""));
        String pics = user.getPicture();
        if(StringUtils.isNotBlank(pics)){
            String[] picArg = pics.split(",");
            pics = picArg[0];
        }else{
            pics = Constants.USER_ICON_DEFAULT;
        }
        this.setUserIcon(pics);
        this.setLogo(user.getLogo());
        this.setWeiDianStatus(user.getWeiDianStatus());
    }

    public UserDTO(User user, String type) {

        // 1: 达人
        if (UserConstants.USER_DTO_TYPE_TALENT.equals(type)) {

            // 用户ID
            this.setUserId(ObjectUtils.toLong(user.getUserId(), 0l));
            // 用户头像
            String pics = user.getPicture();
            if (StringUtils.isNotBlank(pics)) {

                String[] picArg = pics.split(",");
                pics = picArg[0];
            } else {

                pics = Constants.USER_ICON_DEFAULT;
            }
            this.setUserIcon(pics);
            // 用户昵称
            this.setNickName(ObjectUtils.toString(user.getNickName(), ""));
            // 用户介绍
            this.setIntroduce(ObjectUtils.toString(user.getIntroduce(), ""));
            // 达人推荐语
            this.setRecommend(ObjectUtils.toString(user.getRecommend(), ""));
            // 用户角色
            this.setRole(ObjectUtils.toInteger(user.getRole(), 0));
            // 用户性别
            this.setSex(ObjectUtils.toInteger(user.getSex(), 0));
            // 用户粉丝数量
            this.setFollower(ObjectUtils.toLong(user.getFollower(), 0l));
            // 用户关注数量(关注别人)
            this.setFollowing(ObjectUtils.toLong(user.getFollowing(), 0l));
            // 点赞数
            this.setLikeNum(ObjectUtils.toLong(user.getLikeNum(), 0l));
            // 好友关系状态
            this.setStatus(ObjectUtils.toInteger(user.getStatus(), 0));
        } else if (UserConstants.USER_DTO_TYPE_DATA_SYNC.equals(type)) {
            // 2: 数据同步

            this.setUserId(ObjectUtils.toLong(user.getUserId()));
            this.setUserName(ObjectUtils.toString(user.getUserName(), ""));
            this.setNickName(ObjectUtils.toString(user.getNickName(), ""));
            this.setSex(ObjectUtils.toInteger(user.getSex(), 0));
            this.setUserType(ObjectUtils.toInteger(user.getUserType()));
            this.setPicture(ObjectUtils.toString(user.getPicture(), ""));
            this.setEmail(ObjectUtils.toString(user.getEmail(), ""));
            this.setPhone(ObjectUtils.toString(user.getPhone(), ""));
            this.setMobile(ObjectUtils.toString(user.getMobile(), ""));
            this.setUserStatus(ObjectUtils.toInteger(user.getUserStatus(), 1));
            this.setAddress(ObjectUtils.toString(user.getAddress(), ""));
            this.setCreateTime(ObjectUtils.toLong(user.getCreateTime(), 0l));
            this.setUpdateTime(ObjectUtils.toLong(user.getUpdateTime(), 0l));
            this.setLastLoginTime(ObjectUtils.toLong(user.getLastLoginTimeStr(), 0l));
            this.setAccessToken(ObjectUtils.toString(user.getAccessToken(), ""));
            this.setThirdId(ObjectUtils.toString(user.getThirdId(), ""));
            this.setSinaId(ObjectUtils.toString(user.getSinaId(), ""));
            this.setQqId(ObjectUtils.toString(user.getQqId(), ""));
            this.setWeixinId(ObjectUtils.toString(user.getWeixinId(), ""));
            this.setSinaBind(ObjectUtils.toInteger(user.getSinaBind(), 0));
            this.setQqBind(ObjectUtils.toInteger(user.getQqBind(), 0));
            this.setWeixinBind(ObjectUtils.toInteger(user.getWeixinBind(), 0));
            this.setTokenSecret(ObjectUtils.toString(user.getTokenSecret(), ""));
            this.setIntroduce(ObjectUtils.toString(user.getIntroduce(), ""));
            this.setRole(ObjectUtils.toInteger(user.getRole(), 0));
            this.setLevel(ObjectUtils.toInteger(user.getLevel(), 0));
            this.setBroadCastNum(ObjectUtils.toLong(user.getBroadCastNum(), 0L));
            this.setLikeNum(ObjectUtils.toLong(user.getLikeNum(), 0l));

            devIdList = Lists.newArrayList();
            if (user.getDevIdList() != null) {

                devIdList = user.getDevIdList();
            }
            this.setDevIdList(devIdList);
            this.setSsoTk(ObjectUtils.toString(user.getSsoTk(), ""));
            this.setRecommend(ObjectUtils.toString(user.getRecommend(), ""));

            tags = Lists.newArrayList();
            if (user.getTags() != null) {

                tags = user.getTags();
            }
            this.setTags(tags);
        }else if (UserConstants.USER_DTO_TYPE_USER_INVITEDUSER.equals(type)) {
            // 3: 受邀用户列表

            this.setUserId(ObjectUtils.toLong(user.getUserId()));
            this.setNickName(ObjectUtils.toString(user.getNickName(), ""));
            // 用户头像
            String pics = user.getPicture();
            if (StringUtils.isNotBlank(pics)) {

                String[] picArg = pics.split(",");
                pics = picArg[0];
            } else {

                pics = Constants.USER_ICON_DEFAULT;
            }
            this.setUserIcon(pics);
        }
        this.setLogo(user.getLogo());
    }

    public UserDTO() {}

    @Override
    public int hashCode() {
        return this.userId.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof UserDTO) {
            UserDTO userDTO = (UserDTO) obj;
            if (this.userId.equals(userDTO.getUserId())) {
                return true;
            }
        }
        return false;
    }
}
